package com.toe.user.webapi.controller;


import com.toe.common.restful.JsonResult;
import com.toe.pojo.dto.user.UserRoleAddDTO;
import com.toe.pojo.dto.user.UserRolePermissionAddDTO;
import com.toe.pojo.vo.user.UserRoleVO;
import com.toe.user.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/webapi/user-role")
@Api(tags = "角色模块")
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    @ApiOperation("添加角色")
    @PostMapping("/insert")
    public JsonResult<Void> insert(@RequestBody UserRoleAddDTO userRoleAddDTO){
        userRoleService.insert(userRoleAddDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除角色")
    @PostMapping("/delete")
    public JsonResult<Void> insert(long id){
        userRoleService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询全部橘色")
    @GetMapping("/select")
    public JsonResult<List<UserRoleVO>> selectAll(){
        List<UserRoleVO> userRoleVOS = userRoleService.selectAll();
        return JsonResult.ok(userRoleVOS);
    }

    @ApiOperation("查询某个橘色")
    @GetMapping("/select/one")
    public JsonResult<UserRoleVO> selectOne(long roleId){
        UserRoleVO userRoleVO = userRoleService.selectOne(roleId);
        return JsonResult.ok(userRoleVO);
    }

    @ApiOperation("添加权限")
    @PostMapping("/addPermission")
    public JsonResult<Void> addPermission(@RequestBody UserRolePermissionAddDTO userRolePermissionAddDTO){
        long roleId = userRolePermissionAddDTO.getRoleId();
        long permissionId = userRolePermissionAddDTO.getPermissionId();
        userRoleService.addRolePermission(roleId, permissionId);
        return JsonResult.ok();
    }
}

